package com.ljh.mboxvidoserver.console.shoptype.controller;

import com.ljh.mboxvidoserver.common.result.entity.Result;
import com.ljh.mboxvidoserver.console.shoptype.entity.ShopType;
import com.ljh.mboxvidoserver.console.shoptype.service.ShopTypeService;
import com.ljh.mboxvidoserver.system.entity.LoginUser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.Date;

@Controller
@RequestMapping(value = "/shopType")
public class ShopTypeController {

    @Autowired
    private ShopTypeService shopTypeService;

    private Logger logger = LoggerFactory.getLogger(ShopTypeController.class);


    @RequestMapping(value = "/view/{path}")
    public ModelAndView view(@PathVariable(value = "path") String path,ModelAndView model){

        model.setViewName("console/shopType/"+path);

        return model;
    }

    /**
     * 分页查询
     * @param shopType 条件
     * @param pageable 分页参数
     * @param model 模型
     * @return
     */
    @GetMapping(value = "findShopTypePage")
    public ModelAndView findShopTypePage(ShopType shopType, Pageable pageable, ModelAndView model){

        try {
            Page<ShopType> page = shopTypeService.findList(shopType, pageable);

            model.addObject("pager",page);

            model.setViewName("console/shopType/shopType_list");
        }catch (Exception e){
            logger.error(e.getMessage());
            e.printStackTrace();
        }
        return model;
    }

    /**
     *
     * @param option
     * @param id
     * @return
     */
    @GetMapping(value = "findShopType/{option}/{id}")
    public ModelAndView findShopType(@PathVariable(value = "option") String option,@PathVariable(value = "id") Integer id){
        ModelAndView model = new ModelAndView("console/shopType/shopTypeEdit");
        try {

            ShopType shopType = new ShopType();

            shopType.setId(id);

            shopType = shopTypeService.findOne(shopType);

            model.addObject(shopType);
            model.addObject(option);

        } catch (Exception e) {
            logger.error(e.getMessage());
            e.printStackTrace();
        }
        return model;
    }

    @PostMapping(value = "saveShopType")
    @ResponseBody
    public Result saveShopType(ShopType shopType, HttpSession session){
        Result result = new Result();
        try {
            LoginUser loginUser = (LoginUser) session.getAttribute("loginUser");
            shopType.setCreateUser(loginUser);
            shopType.setCreateTime(new Date());
            shopTypeService.insertOne(shopType);
            result.setMessage("保存分类成功");
            result.setResult("success");
            result.setStatus(Result.SUCCESS);
        }catch(Exception e){
            result.setStatus(Result.FAIL);
            result.setResult("success");
            result.setMessage("保存失败");
            logger.error(e.getMessage());
        }
        return result;
    }

    @PostMapping(value = "deleteShopType")
    @ResponseBody
    public Result deleteShopType(ShopType shopType){
        Result result = new Result();
        try {
            shopTypeService.deleteOne(shopType);
            result.setMessage("删除分类成功");
            result.setResult("success");
            result.setStatus(Result.SUCCESS);
        }catch(Exception e){
            result.setStatus(Result.FAIL);
            result.setResult("success");
            result.setMessage("删除失败");
            logger.error(e.getMessage());
        }
        return result;
    }

    @PostMapping(value = "updateShopType")
    @ResponseBody
    public Result updateShopType(ShopType shopType, HttpSession session){
        Result result = new Result();
        try {
            LoginUser loginUser = (LoginUser) session.getAttribute("loginUser");
            shopType.setUpdateUser(loginUser);
            shopType.setUpdateTime(new Date());
            shopTypeService.updateOne(shopType);
            result.setMessage("分类修改成功");
            result.setResult("success");
            result.setStatus(Result.SUCCESS);
        }catch(Exception e){
            result.setStatus(Result.FAIL);
            result.setResult("success");
            result.setMessage("修改失败");
            logger.error(e.getMessage());
        }
        return result;
    }
}
